package com.cheksin.controller;

import com.cheksin.entity.ElemeAddGoods;
import com.cheksin.entity.Goods;
import com.cheksin.entity.GoodsHtml;
import com.cheksin.excl.*;
import com.cheksin.url.YinBaoCookieUtil;
import com.cheksin.url.YinBaoHtmlCookieUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 不愿透露
 * @Classname IndexController
 * @Description TODO
 * @Date 2021/10/25 13:41
 */
@Controller
public class ExclController {
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    @GetMapping("/index")
    public String index(){
        return "print";
    }

    /**
     * @param file
     * @return java.lang.String
     * @author 不愿透露
     * @date 银豹 18:01
     */
    @PostMapping("/upload")
    @ResponseBody
    @CrossOrigin
    public List<List<Goods>> upload(@RequestParam("file") MultipartFile file) {

        try {
            List<List<Goods>> json = new ArrayList<>();
            List<Goods> goods = ReadExcelUtil.readExcelInfo(FileUtil.multipartFileToFile(file));
            int rows=goods.size();
            //每页显示的记录数
            int pageSize=8;
            //页数
            int pageSum=(rows-1)/pageSize+1;
            for(int index =1;index<=pageSum;index++) {
                json.add(PageUtil.startPage(goods,index,pageSize));
            }
            return json;
        } catch (Exception e) {
            System.out.println(e);
           return null;
        }
    }

    @PostMapping("/exportXlsx")
    @ResponseBody
    public String exportXlsx(@RequestParam("file") MultipartFile file,String path) throws ParseException {
        path = (path == null || "".equals(path)) ? "D:\\" : path;
        String exportPath = path+"商品资料-back.xls";

        System.out.println(exportPath);
        String[] header = {"名称（必填）", "分类（必填）", "条码","扩展条码", "主编码", "规格", "主单位", "库存量", "进货价（必填）", "销售价（必填）", "毛利率", "批发价", "会员价", "会员折扣", "积分商品",
                "库存上限", "库存下限", "库位", "品牌", "供货商", "生产日期", "保质期", "拼音码", "货号", "市场价", "过期日", "自定义3", "重量", "是否称重","是否传秤","是否计数商品","称编码", "商品状态", "商品描述", "创建日期"};
        try {
            ExportXlsx.exportExcl(header,ExportXlsx.genGoods(FileUtil.multipartFileToFile(file)), exportPath,new Goods());
        }catch (Exception e) {
            return e.toString();
        }

        /*List<Goods> goods = (List<Goods>) ExportXlsx.genGoods(readFile);
        List<Goods> copy = new ArrayList<>();
        List<String> name = ReadText.readFileContent("d:\\降价商品.txt");
        for (Goods good : goods) {
            if(name.indexOf(good.getMc()) > -1) {
                copy.add(good);
            }
        }
        ExportXlsx.exportExcl(header,copy, exportPath);*/
        return exportPath;
    }

    /**
     *导出饿了么商品信息
     * @author 不愿透露
     * @date 2021/12/9 20:33
     * @param file
     * @param path
     * @return java.lang.String
     */
    @PostMapping("/exportElemeXlsx")
    @ResponseBody
    public String exportElemeXlsx(@RequestParam("file") MultipartFile file,String path,String erportType ,Integer elemeType) throws ParseException {
        path = (path == null || "".equals(path)) ? "D:\\饿了么" : path;
        File files = new File(path);
        if(!files.exists()) {
            files.mkdir();
        }
        String exportPath = path+"\\饿了么.xls";
        //erportType 1新增模版 2 修改模版

        System.out.println(exportPath);
        String[] header = {"商品条形码", "商品名称", "商品三级类目","重量", "售价", "库存", "商品最低起购量", "商品状态", "店铺内一级分类名称", "店铺内二级分类名称", "商品自定义ID", "货架号",
                "限购开始时间", "限购结束时间", "限购周期", "限购周期内每人限购件数"};
        try {
            ExportXlsx.exportExcl(header,ExportElemeXlsx.genGoods(FileUtil.multipartFileToFile(file)), path+"\\饿了么.xls",new ElemeAddGoods());
            if(elemeType == 2) {
                //需要导出图片
                YinBaoCookieUtil.exportImg(path,FileUtil.multipartFileToFile(file),elemeType);
            }
        }catch (Exception e) {
            return e.toString();
        }
        return exportPath;
    }

    /**
     *导出全部图片
     * @author 不愿透露
     * @date 2021/12/4 14:41
     * @param file
     * @param imgPath
     * @return java.lang.String
     */
    @PostMapping("/exportImg")
    @ResponseBody
    public String exportImg(@RequestParam("file") MultipartFile file,String imgPath) throws ParseException {
        imgPath = (imgPath == null || "".equals(imgPath)) ? "D:\\银豹图片下载" : imgPath;
        String exportPath = imgPath;
        try {
            return YinBaoCookieUtil.exportImg(exportPath,FileUtil.multipartFileToFile(file),1);
        }catch (Exception e) {
            return "C:"+e.toString();
        }
    }

    @PostMapping("/exportDateXlsx")
    @ResponseBody
    public List<Goods> exportDateXlsx(@RequestParam("file") MultipartFile file,String dateSum) throws ParseException {
        String[] header = {"名称（必填）", "分类（必填）", "条码","扩展条码", "主编码", "规格", "主单位", "库存量", "进货价（必填）", "销售价（必填）", "毛利率", "批发价", "会员价", "会员折扣", "积分商品",
                "库存上限", "库存下限", "库位", "品牌", "供货商", "生产日期", "保质期", "拼音码", "货号", "市场价", "过期日", "自定义3", "重量", "是否称重","是否传秤","是否计数商品","称编码", "商品状态", "商品描述", "创建日期"};
        try {
            dateSum = (null == dateSum || "".equals(dateSum)) ? "30" : dateSum;
            int negativeVal = (~(Integer.valueOf(dateSum) - 1));
            List<Goods> goods = (List<Goods>) ExportXlsx.genGoods(FileUtil.multipartFileToFile(file));
            List<Goods> goodsDate = new ArrayList<>();
            for (Goods good : goods) {
                //过期筛选
                if(Integer.valueOf(good.getKcl()) > 0) {
                    String gqr = good.getGqr();
                    Date dates = new Date();
                    Calendar calc = Calendar.getInstance();
                    calc.setTime(SIMPLE_DATE_FORMAT.parse(gqr));
                    calc.add(Calendar.DAY_OF_MONTH, negativeVal);
                    if(dates.compareTo(calc.getTime()) > 0) {
                        System.out.println(good);
                        goodsDate.add(good);
                    }
                }
            }
            goodsDate.sort((t1, t2) -> t1.getGqr().compareTo(t2.getGqr()));

            return goodsDate;
        }catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    }

    @RequestMapping("/exportHtmlIndex")
    public String exportHtmlIndex(){
        return "exportHtml";
    }

    /**
     *
     * @author 不愿透露
     * @date 生成模版 16:07
     * @return java.lang.String
     */
    @RequestMapping("/exportHtml")
    @ResponseBody
    public List<GoodsHtml> exportHtml(String start,String end){
        String json = "error";
        try {
            Date date = new Date();
            start = (start == null || "".equals(start)) ? SIMPLE_DATE_FORMAT.format(date) : start;
            end = (end == null || "".equals(end)) ? SIMPLE_DATE_FORMAT.format(end) : end;
            SIMPLE_DATE_FORMAT.format(start);
            SIMPLE_DATE_FORMAT.format(end);

        }catch (Exception e) {
            System.out.println(e);
        }
        List<GoodsHtml> goodsHtmls = YinBaoHtmlCookieUtil.expoerHtml(start,end);
        return goodsHtmls;

    }
}